package com.umiwe.permission.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import com.umiwe.entity.UmiweUser;
import org.apache.ibatis.annotations.*;

import java.util.ArrayList;
import java.util.List;

@Mapper
public interface UserMapper extends BaseMapper<UmiweUser> {

    /**
     * 新增用户信息
     * @param umiweUser
     * @return
     */
    @Insert("INSERT INTO umiwe_user(unionid,user_name,password,dingtalk_id,department_id,create_time,user_status,is_delete,china_user_name) VALUES" +
            "(#{umiweUser.unionid},#{umiweUser.userName},#{umiweUser.password},#{umiweUser.dingtalkId},#{umiweUser.departmentId},NOW(),#{umiweUser.userStatus},#{umiweUser.isDelete}" +
            ",#{umiweUser.chinaUserName})")
    boolean addUser(@Param("umiweUser")UmiweUser umiweUser);

    @Select("select dingtalk_id as dingtalkId,user_name as userName,department_id as departmentId FROM umiwe_user where department_id = #{departmentId}")
    ArrayList<UmiweUser> getUserDepartmentList(Integer departmentId);

    @Update("UPDATE umiwe_user SET user_name = #{umiweUser.userName}, update_time = NOW(), china_user_name = #{umiweUser.chinaUserName} WHERE dingtalk_id = #{umiweUser.dingtalkId}")
    boolean updateDate(@Param("umiweUser")UmiweUser umiweUser);

    @Update("update  umiwe_user SET user_status = 3 ,is_delete = 1, update_time = NOW() where dingtalk_id=#{dingtalkId}")
    boolean deleteDate(@Param("dingtalkId")String dingtalkId);

    /**
     * 查询有没有
     * @param dingTalkId
     * @return
     */
    @Select("select EXISTS(SELECT 1 FROM umiwe_user WHERE dingtalk_id=#{dingTalkId})")
    boolean getUserDingTalkId(String dingTalkId);

    /**
     * 扫码的 时候 增加值
     * @param unionid
     * @param openid
     * @param userId
     * @return
     */
    @Update("update umiwe_user set openid = #{openid},unionid = #{unionid} where dingtalk_id = #{userId}")
    boolean updateUserData(String unionid,String openid,String userId);

    /**
     * 按dingding ID 查询用户信息
     * @param dingTalkId
     * @return
     */
    @Select("select id,dingtalk_id as dingtalkId,user_name as userName,department_id as departmentId,password FROM umiwe_user where dingtalk_id = #{dingTalkId}")
    UmiweUser getDingTalkId(String dingTalkId);

    /**
     * 修改密码
     * @param password
     * @param dingTalkId
     * @return
     */
    @Update("UPDATE umiwe_user SET password = #{password} WHERE dingtalk_id = #{dingTalkId}")
    boolean updatePassWord(String password,String dingTalkId);

    /**
     * ding id查询用户id
     * @param dingId
     * @return
     */
    @Select("select id from umiwe_user where dingtalk_id=#{dingTalkId}")
    Integer getUserId(String dingId);

    /**
     * 获取用户id
     * @param id
     * @return
     */
    @Select("select * from umiwe_user where id=#{id}")
   UmiweUser getUserById(Integer id);

    /**
     * 	修改用户自身信息
     * @param user
     * @return
     */
    int updateUser(UmiweUser user);
}
